//
//  DEDefaultTabBarController.m
//  DemoObjective-CWeb
//
//  Created by Mon on 15/12/13.
//  Copyright © 2015年 Mon. All rights reserved.
//

#import "DEDefaultTabBarController.h"
#import "DEHomeTableViewController.h"
#import "DEMessageTableViewController.h"
#import "DEDiscoverTableViewController.h"
#import "DEProfileTableViewController.h"
#import "DETabBar.h"

@interface DEDefaultTabBarController ()

@property (nonatomic,strong) DEHomeTableViewController *homeVC;

@property (nonatomic,strong) DEMessageTableViewController *messageVC;

@property (nonatomic,strong) DEDiscoverTableViewController *discoverVC;

@property (nonatomic,strong) DEProfileTableViewController *profileVC;

@end

@implementation DEDefaultTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    DETabBar *newTabBar = [[DETabBar alloc] initWithFrame:self.tabBar.frame];
    
    [self setValue:newTabBar forKey:@"tabBar"];
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    [self prepareControllersWithController:self.homeVC andtitleStirng:@"首页" ImageName:@"tabbar_home"];
    
    [self prepareControllersWithController:self.messageVC andtitleStirng:@"消息" ImageName:@"tabbar_message_center"];
    
    [self prepareControllersWithController:self.discoverVC andtitleStirng:@"发现" ImageName:@"tabbar_discover"];
    
    [self prepareControllersWithController:self.profileVC andtitleStirng:@"我" ImageName:@"tabbar_profile"];
}

/// 封装重复代码
- (void)prepareControllersWithController:(UITableViewController *)controller andtitleStirng:(NSString *)titleString ImageName:(NSString *)imageName {
    
    UINavigationController *nagVC = [[UINavigationController alloc] initWithRootViewController:controller];
    controller.title = titleString;
    nagVC.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 去掉图片的系统渲染
    nagVC.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:nagVC];
}

#pragma mark - 懒加载视图控制器
- (DEHomeTableViewController *)homeVC {
    if (!_homeVC) {
        _homeVC = [[DEHomeTableViewController alloc] init];
    }
    return _homeVC;
}

- (DEMessageTableViewController *)messageVC {
    if (!_messageVC) {
        _messageVC = [[DEMessageTableViewController alloc] init];
    }
    return _messageVC;
}

- (DEDiscoverTableViewController *)discoverVC {
    if (!_discoverVC) {
        _discoverVC = [[DEDiscoverTableViewController alloc] init];
    }
    return _discoverVC;
}

- (DEProfileTableViewController *)profileVC {
    if (!_profileVC) {
        _profileVC = [[DEProfileTableViewController alloc] init];
    }
    return _profileVC;
}

@end
